from datetime import datetime

import yaml


def ReadConfigs(yaml_path="./configs.yaml"):
    with open(yaml_path, 'r') as file:
        configs = yaml.load(file, Loader=yaml.FullLoader)
    return configs


def WriteArray(my_array, file_name):
    with open(file_name, 'w') as f:
        for i in my_array:
            for j in i:
                f.write(str(j))
                f.write(' ')
            f.write('\n')
        f.close()


def ReadIntArray(file_name):
    file = open(file_name, mode='r', encoding='UTF-8')
    my_array = []
    contents = file.readlines()
    for msg in contents:
        msg = msg.strip('\n')
        msg = msg.strip()
        adm = msg.split(' ')
        adm = [int(x) for x in adm]
        my_array.append(adm)
    file.close()
    return my_array


def ReadFloatArray(file_name):
    file = open(file_name, mode='r', encoding='UTF-8')
    my_array = []
    contents = file.readlines()
    for msg in contents:
        msg = msg.strip('\n')
        msg = msg.strip()
        adm = msg.split(' ')
        adm = [float(x) for x in adm]
        my_array.append(adm)
    file.close()
    return my_array


def afterList(index):
    my_list = []
    for i in range(1, 34):
        if i in index:
            my_list.append(1)
        else:
            my_list.append(0)
    return my_list


def OneHotData(data):
    data1 = []
    length = len(data)
    for i in range(length):
        a = afterList(data[i])
        data1.append(a)
    return data1


def GetConfusionMatrix(list1, list2):
    TP = 0
    FP = 0
    TN = 0
    FN = 0
    for i in range(list1.numel()):
        if (list1[i] == list2[i]) and (list2[i] == 0):
            TP += 1
        elif (list1[i] == list2[i]) and (list2[i] == 1):
            TN += 1
        elif (list1[i] != list2[i]) and (list2[i] == 0):
            FP += 1
        elif (list1[i] != list2[i]) and (list2[i] == 1):
            FN += 1
    return TP, FP, TN, FN


def PrintLog(message):
    t = datetime.now().strftime("%d/%m/%y - %H:%M:%S")
    print(f"({t}): {message}")
    # unfinished.


def GetParamList():
    param_lists = []
    for index in range(1, 34):
        param_name = 'param/param_' + str(index) + '.txt'
        tmp_list1 = ReadFloatArray(param_name)
        [list1] = tmp_list1
        param_lists.append(list1)
    return param_lists
